<template>
  <view class="mobile-item-container">
    <Navbar :title="id ? '修改' : '新增'" bgColor="#fff" :h5Show="false"></Navbar>
	<uni-section :title="id ? '修改' : '新增'" type="line">
		<view class="example">
			<uni-forms ref="form" :model="formData" :rules="rules" labelWidth="80px">
				<uni-forms-item label="销售员" required name="userId">
					<uni-data-select v-model="formData.userId" :localdata="actions"  :disabled="disable">
					</uni-data-select>
				</uni-forms-item>
				 <uni-forms-item label="销售日期" required name="salesTime">
				 	<uni-datetime-picker type="date" return-type="timestamp"  v-model="formData.salesTime" />
				 </uni-forms-item>
				  
				 <uni-forms-item label="销售渠道" required name="userChannel">
				 	<uni-data-checkbox v-model="formData.userChannel" :localdata="userChannels" />
				 </uni-forms-item>
				 
				 <uni-forms-item label="所属分组" required name="groupsType">
				 	<uni-data-checkbox v-model="formData.groupsType" :localdata="groupsTypes" />
				 </uni-forms-item>
				 
				 <uni-forms-item label="销售总额" required name="salesTotal">
				 	<uni-easyinput v-model="formData.salesTotal" placeholder="请输入销售总额" />
				 </uni-forms-item>

				<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="formData.remark" placeholder="请输入备注" />
				</uni-forms-item>
			</uni-forms>
			<view class="button-group">
				<u-row :gutter="12">
				  <u-col :span="4">
				    <u-button v-if="id" type="error" text="删除" @click="del" v-hasPermi="['goods:storageSalesInfo:remove']"></u-button>
				  </u-col>
				  <u-col :span="4">
				    <u-button icon="arrow-left" text="返回" plain @click="goBack()"></u-button>
				  </u-col>
				  <u-col :span="4">
					  <u-button v-if="!id" type="primary" text="新增" @click="submit" v-hasPermi="['goods:storageSalesInfo:add']"></u-button>
					  <u-button  v-else type="primary" text="修改" @click="submit" v-hasPermi="['goods:storageSalesInfo:edit']"></u-button>
				  </u-col>
				</u-row>
			</view>
		</view>
	</uni-section>
	<buttom-tabbar></buttom-tabbar>
  </view>
</template>

<script>
import * as StorageSalesInfoApi from '@/api/goods/storageSalesInfo'
import * as DictInfoApi from '@/api/system/dict/data'
import Navbar from '@/components/navbar/Navbar'
import * as UserInfoApi from '@/api/user'

export default {
  components: {
    Navbar,
  },
  data () {
    return {
      id: undefined,
      formData:{
			id: null,
			userId: null,
			salesTime: null,
			userChannel: null,
			groupsType: null,
			salesTotal: null,
			remark: null,
			createBy: null,
			createTime: null,
			updateBy: null,
			delFlag: null,
			updateTime: null
        },
	  actions: [],
	  actionShow: false,
	  nickName:"",
	  actionTitle: '',
	  // 校验规则
	  rules: {
	  	userId: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '销售员不能为空'
	  		}]
	  	},
		salesTime: {
			rules: [{
				required: true,
				errorMessage: '销售日期不能为空'
			}]
		},
		userChannel: {
			rules: [{
				required: true,
				errorMessage: '销售渠道不能为空'
			}]
		},
		groupsType: {
			rules: [{
				required: true,
				errorMessage: '所属分组不能为空'
			}]
		},
	  	salesTotal: {
	  		rules: [{
	  			required: true,
	  			errorMessage: '销售总额不能为空'
	  		}, {
	  			format: 'number',
	  			errorMessage: '销售总额只能输入数字'
	  		}]
	  	} 
		 ,
	  },
	  showCalendar:false,
	  // 单选数据源
	  userChannels: [],
	  groupsTypes:[],
	  disable:true
    }
  },

  onLoad (params) {
    this.id = params.id
	this.selectUserListByPostCode();
    this.loadData()
	this.getDictArray("user_channel")
	this.getDictArray("groups_type")
	if(this.$auth.hasRole("system") || this.$auth.hasRole("admin"))
	{
		this.disable = false;
	}
  },
  methods: {
    loadData () {
      if (this.id) {
        const app = this
        StorageSalesInfoApi.getStorageSalesInfo(this.id).then(res => {
			if(res.data)
			{
				app.formData = res.data
				app.formData.userId = res.data.userId+""
			}
        })
      }else
	  {
		  this.formData.userId = this.$store.getters.user.userId+""
	  }
    },
	getDictArray(type)
	{
		DictInfoApi.getDicts(type).then(res => {
			if(res.data)
			{
				if(res.data.length)
				{
					for(let i=0;i<res.data.length;i++)
					{
						let temp = res.data[i];
						let obj = {};
						obj.value = temp.dictValue;
						obj.text = temp.dictLabel
						if(type==="user_channel")
						{
							this.userChannels.push(obj); 
						}else if(type==="groups_type")
						{
							this.groupsTypes.push(obj); 
						}
						
					}
				}
			}
		});
	},
	selectUserListByPostCode()
	{
		UserInfoApi.selectUserDict().then((response) => {
				if(response.rows)
				{
					if(response.rows.length)
					{
						for(let i=0;i<response.rows.length;i++)
						{
							let temp = response.rows[i];
							let obj = {};
							obj.value = temp.userId;
							obj.text = temp.nickName
							this.actions.push(obj);
						}
					}
				}
		});
	},
    del() {
		let id = this.id
		uni.showModal({
			title: '提示',
			content: '确定要删除吗？',
			success: function (res) {
				if (res.confirm) {
					StorageSalesInfoApi.delStorageSalesInfo(id).then(res2 => {
					  uni.showToast({ title: '提交成功！' })
					  uni.navigateTo({ url: '/pages/goods/storageSalesInfo/index'})
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
    },
    submit() {
		this.$refs.form.validate().then(res => {
		  if (this.id) {
		    StorageSalesInfoApi.updateStorageSalesInfo(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
					this.goBack()
		    })
		  } else {
		    StorageSalesInfoApi.addStorageSalesInfo(this.formData).then(res => {
		      uni.showToast({ title: '提交成功！' })
					this.goBack()
		    })
		  }
		});
    },
    
    goBack() {
		uni.navigateTo({ url: '/pages/goods/storageSalesInfo/index'})
    },
  }
}
</script>
<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>
